package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.UserAuthDto;

public class UserAuthDaoImp extends TmsDao implements UserAuthDao {

	protected UserAuthDaoImp() throws TmsDaoException {
		super();
	}
	/**
	 * 根据用户ID获得对应的RolID
	 * @param userId
	 * @return
	 * @throws TmsDaoException
	 */
	@Override
	public List<UserAuthDto> getRolId(int userId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		UserAuthDto dto=null;
		List<UserAuthDto>  list = new ArrayList<UserAuthDto>();
		try {
			conn = super.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT   ");
			sql.append("ID     , ");
			sql.append("USERID, ");
			sql.append("RID     ");
			sql.append("FROM    ");
			sql.append(TmsTableNames.TMS_USER_AUTH);
			sql.append("   WHERE   ");
			sql.append("USERID=?   ");
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1,userId);
			// 执行sql
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new UserAuthDto();
				dto.setId(rs.getInt("ID"));
				dto.setrId(rs.getInt("RID"));
				dto.setUserId(rs.getInt("USERID"));
				list.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());	
		} finally {
			close(rs,ps);
		}
		return list;
	}
	/**
	 * 添加权限信息
	 */
	@Override
	public void insertUserAuthInfo(int userId, int ucId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_USER_AUTH);
			sql.append("(");
			sql.append(" USERID, ");
			sql.append(" RID     ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,? )");

			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1,userId);
			ps.setInt(2,ucId);
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		
	}
	/**
	 * 
	 * 根据user表主键ID删除auth信息
	 */
	@Override
	public void delUserAuthInfo(int userId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("DELETE ");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_USER_AUTH);
			sql.append(" WHERE USERID = "+userId+"  ");

			ps = conn.prepareStatement(sql.toString());
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}		
	}
	@Override
	public void deleteAuth(int rId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("DELETE ");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_USER_AUTH);
			sql.append(" WHERE RID = "+rId+"  ");

			ps = conn.prepareStatement(sql.toString());
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}		
		
	}
	@Override
	public UserAuthDto getAuthInfoByRole(int userid, int roleid)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		UserAuthDto dto=null;
		try {
			conn = super.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT   ");
			sql.append("ID     , ");
			sql.append("USERID, ");
			sql.append("RID     ");
			sql.append("FROM    ");
			sql.append(TmsTableNames.TMS_USER_AUTH);
			sql.append(" WHERE   ");
			sql.append(" USERID="+userid+"   ");
			sql.append(" AND RID="+roleid+"   ");
			ps = conn.prepareStatement(sql.toString());
			// 执行sql
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new UserAuthDto();
				dto.setId(rs.getInt("ID"));
				dto.setrId(rs.getInt("RID"));
				dto.setUserId(rs.getInt("USERID"));
			}
			return dto;
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());	
		} finally {
			close(rs,ps);
		}
	}

}

